package com.example.administrator.reader.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.reader.R;
import com.example.administrator.reader.entity.City;
import com.example.administrator.reader.entity.Novel;
import com.example.administrator.reader.fragment.CityFragment;
import com.example.administrator.reader.fragment.ShelfFragment;
import com.example.administrator.reader.utils.BitmapHelper;
import com.example.administrator.reader.view.SListView;
import com.lidroid.xutils.BitmapUtils;

import java.util.ArrayList;
import java.util.List;

public class CitySearch extends Activity implements View.OnClickListener {

    private ImageView iv_face;
    private ImageView iv_search;
    private EditText et_content;
    private SListView lv;
    public BitmapUtils bitmapUtils;

    private CitySearchAdapter citySearchAdapter;
    private String seachContent;

    private List<Novel> zhudas;
    private List<Novel> gudians;
    private List<Novel> frees;
    private List<Novel> novels = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setUpContentView();
        setUpView();
        setUpData();

    }


    private void setUpContentView() {
        setContentView(R.layout.activity_city_search);
        bitmapUtils = BitmapHelper.getBitmapUtils();
    }

    private void setUpView() {
        iv_face = (ImageView) findViewById(R.id.iv_face);
        iv_search = (ImageView) findViewById(R.id.iv_search);
        et_content = (EditText) findViewById(R.id.et_content);
        iv_face.setOnClickListener(this);
        iv_search.setOnClickListener(this);
        lv = (SListView) findViewById(R.id.lv);
    }

    private void setUpData() {
        zhudas = CityFragment.cityData.getZhudas();
        gudians = CityFragment.cityData.getGudians();
        frees = CityFragment.cityData.getFrees();

        citySearchAdapter = new CitySearchAdapter();
        lv.setAdapter(citySearchAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_face:
                finish();
                break;

            case R.id.iv_search:
                // 搜索
                searchFunc();
                break;
        }
    }

    private void searchFunc() {
        seachContent = et_content.getText().toString();
        if (!seachContent.isEmpty()) {
            novels.clear();
            for (Novel novel : zhudas) {
                if (novel.getName().indexOf(seachContent) >= 0) {
                    novels.add(novel);
                }
            }
            for (Novel novel : gudians) {
                if (novel.getName().indexOf(seachContent) >= 0) {
                    novels.add(novel);
                }
            }
            for (Novel novel : frees) {
                if (novel.getName().indexOf(seachContent) >= 0) {
                    novels.add(novel);
                }
            }
            citySearchAdapter.notifyDataSetChanged();
        }
    }

    class CitySearchAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return novels.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                convertView = View.inflate(CitySearch.this, R.layout.item_zhuda, null);
                holder = new Holder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv_book_cover);
                holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
                holder.tvAuthor = (TextView) convertView.findViewById(R.id.tv_author);
                holder.tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            Novel novel = novels.get(position);
            bitmapUtils.display(holder.iv, novel.getImgPath());
            holder.tvTitle.setText(novel.getName());
            holder.tvAuthor.setText(novel.getAuthor());
            holder.tvDesc.setText(novel.getDesc());
            return convertView;
        }
    }

    public class Holder {
        public ImageView iv;
        public TextView tvTitle, tvAuthor, tvDesc;
    }
}
